package codeTop;

/**
 * q91
 *
 * @author 雨凡
 * @version q91.java, v0.1
 * @date 2025/07/01/21:57
 */
public class q91 {
    //超时
    public int numDecodings1(String s) {
        dfs(s,0,false);
        return res;
    }
    int res = 0;
    public void dfs(String s,int index,boolean flag) {
        if (index == s.length()) {
            if (!flag) {
                res++;
            }
            return;
        }
        if (flag) {
            int i = Integer.parseInt(s.substring(index - 1, index + 1));
            if (i > 0 && i <= 26) {
                dfs(s,index+1,false);
            }
        }else {
            if (s.charAt(index) != '0') {
                //直接放给下一位处理
                dfs(s,index+1,true);
                //在这一位处理
                dfs(s,index+1,false);
            }
        }
    }

    public int numDecodings(String s) {
        if (s.charAt(0) == '0') {
            return 0;
        }
        int num1 = 1;
        int num2 = 1;
        for (int i = 1; i < s.length(); i++) {
            int num = 0;
            if (s.charAt(i) != '0') {
                num+=num1;
            }
            int temp = (s.charAt(i-1)-'0')*10+(s.charAt(i)-'0');
            if (temp >= 10 && temp <= 26) {
                num+=num2;
            }
            num2 = num1;
            num1 = num;
        }
        return num1;
    }

    public static void main(String[] args) {
        q91 q = new q91();
        System.out.println(q.numDecodings("10"));
    }
}
